package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.ChargeType;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 收费和保证金(客户费用收费退记录表)
 * BT_ITEM_FARE_DETAIL	===> nd_fee_collection_record_table	存储的是收费、退费记录
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_FEE_COLLECTION_RECORD_TABLE(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, CHARGE_TYPE, RETREAT_TYPE, AMOUNT_MONEY, CHARGE_TIME, REFUND_TIME, PAY_ACCOUNT, REMARK, AMOUNT_RECEIVABLE, EXPLANATION, PROCESS_ID, IS_VALID, PAYMENT_ACCOUNT, PAYMENT_TIME, PAYMENT_BANK, CHARGE_TAG, INVOICE_STATUS, TAX_RATE, PAYMENT_BANK_SUBBRANCH) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_FEE_COLLECTION_RECORD_TABLE SET CREATE_NAME = ?, CREATE_BY = ?, CREATE_DATE = ?, UPDATE_NAME = ?, UPDATE_BY = ?, UPDATE_DATE = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, BPM_STATUS = ?, PRO_ID = ?, CHARGE_TYPE = ?, RETREAT_TYPE = ?, AMOUNT_MONEY = ?, CHARGE_TIME = ?, REFUND_TIME = ?, PAY_ACCOUNT = ?, REMARK = ?, AMOUNT_RECEIVABLE = ?, EXPLANATION = ?, PROCESS_ID = ?, IS_VALID = ?, PAYMENT_ACCOUNT = ?, PAYMENT_TIME = ?, PAYMENT_BANK = ?, CHARGE_TAG = ?, INVOICE_STATUS = ?, TAX_RATE = ?, PAYMENT_BANK_SUBBRANCH = ? WHERE ID = ?",
        findMigratedIdListSql = " select ID from ND_FEE_COLLECTION_RECORD_TABLE where PRO_ID = ?",
        seq = 208
)
public class NdFeeCollectionRecordTable extends AbstractBusiness {

    public NdFeeCollectionRecordTable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {

        List<Map> btItemFareDetailList = this.sourceData.getBtItemFareDetail();

        for (Map<String,Object> btItemFareDetail : btItemFareDetailList) {
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(buildId(btItemFareDetail));
            if(needMigratedResult == 0) {
                continue;
            }

            // 单条插入或更新
            if(needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql,this.buildValues(btItemFareDetail),true);
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchUpdateValues.add(this.buildValues(btItemFareDetail));
            } else {
            this.targetDbUtil.updateByParams(insertSql,this.buildValues(btItemFareDetail));
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btItemFareDetail));
            }
        }


    }

    public List buildValues(Map btItemFareDetail) throws Exception{

        List values = new ArrayList();

        //ID	主键
        values.add(buildId(btItemFareDetail));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemFareDetail.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemFareDetail.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(null);
        //PRO_ID	项目ID
        values.add(this.projectId);
        //CHARGE_TYPE	收费类型
        values.add(DictTool.getTargetDictCode(ChargeType.class,btItemFareDetail.get("CHARGETYPE").toString()));
        //RETREAT_TYPE	收退类型 旧系统： 1-收费,2-退费;新系统：charge-收费,refund-退费
        values.add("1".equals(btItemFareDetail.get("IOTYPE").toString()) ? "charge" : "refund");
        //AMOUNT_MONEY	实收额|元
        values.add(btItemFareDetail.get("REALFARE"));
        //CHARGE_TIME	收费时间
        values.add(btItemFareDetail.get("DEALTIME"));
        //REFUND_TIME	退费时间
        values.add(btItemFareDetail.get("DEALTIME"));
        //PAY_ACCOUNT	付款户名
        values.add(btItemFareDetail.get("PAYMENTNAME"));
        //REMARK	备注
        values.add(btItemFareDetail.get("MEMO") == null ? null : Tool.replaceBrFlagStr(Tool.decodeUnicode(btItemFareDetail.get("MEMO").toString())));
        //AMOUNT_RECEIVABLE	应收额|元
        values.add(btItemFareDetail.get("OUGHTFARE"));
        //EXPLANATION	说明 todo
        values.add(null);
        //PROCESS_ID	流程ID
        values.add(btItemFareDetail.get("WORKFLOWNO") == null ? null : btItemFareDetail.get("WORKFLOWNO").toString()  + "-JW");
        //IS_VALID	是否使用
        values.add("Y");
        //PAYMENT_ACCOUNT	付款账号
        values.add(btItemFareDetail.get("PAYACCOUNT"));
        //PAYMENT_TIME	付款时间
        values.add(btItemFareDetail.get("DEALTIME"));
        //PAYMENT_BANK	付款银行 TODO
        values.add(null);
        //CHARGE_TAG	收费标记
        values.add(1);
        //INVOICE_STATUS	发票生成状态 TODO
        values.add(null);
        //TAX_RATE	税率 TODO
        values.add(null);
        //PAYMENT_BANK_SUBBRANCH	支行名称 TODO
        values.add(null);


        return values;
    }

    public String buildId(Map btItemFareDetail){
        // 基础表数据业务主键
        String originalId = btItemFareDetail.get("CHARGENO").toString();
        return originalId;
    }
}
